package com.xhx.accounting.ui.notifications;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.xhx.accounting.DBDao;
import com.xhx.accounting.ListViewAdapter;
import com.xhx.accounting.R;
import com.xhx.accounting.accountbook;
import com.xhx.accounting.add_plan;
import com.xhx.accounting.databinding.FragmentNotificationsBinding;
import com.xhx.accounting.plan_detail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;

    private DBDao db;
    protected List<Map<String, Object>> mSavePlan;
    private ListView plan_lv;
    private SavePlanAdapter mAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        db = new DBDao(getActivity());
        mSavePlan = db.getSavePlan();
        plan_lv = root.findViewById(R.id.save_plan_lv);
        View emptyView = root.findViewById(R.id.empty);
        plan_lv.setEmptyView(emptyView);

//        final TextView textView = binding.textNotifications;
//        notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
        db = new DBDao(getActivity());
        plan_lv = root.findViewById(R.id.save_plan_lv);
        mAdapter = new SavePlanAdapter(getActivity(), mSavePlan);
        plan_lv.setAdapter(mAdapter);

        plan_lv.setOnItemClickListener((parent, view, position, id) -> {
            HashMap<String, Object> listinfo = (HashMap<String, Object>) plan_lv.getItemAtPosition(position);//SimpleAdapter返回Map
            Log.d("onClick",listinfo.toString());
            int planId = (int)listinfo.get("id");
            Intent intent = new Intent();
            intent.setClass(getActivity(), plan_detail.class);//this前面为当前activty名称，class前面为要跳转到得activity名称
            intent.putExtra("planId", planId);
            startActivity(intent);
        });

        plan_lv.setOnItemLongClickListener((parent, view, position, id) -> {
            HashMap<String, Object> listinfo = (HashMap<String, Object>) plan_lv.getItemAtPosition(position);//SimpleAdapter返回Map
            int savePlanId = (int) listinfo.get("id");
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage("此操作不可撤销")
                    .setTitle("确定删除此计划吗？");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK button
                    db.deleteSavePlan(savePlanId);
                    mSavePlan.clear();
                    mSavePlan.addAll(db.getSavePlan());
                    mAdapter.notifyDataSetChanged();
                    onStart();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
            builder.show();
            return true;
        });

        ImageView fab = root.findViewById(R.id.add_plan);
        fab.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(getActivity(), add_plan.class);
            startActivity(intent);
        });

        return root;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    public void onStart() {
        super.onStart();
        mSavePlan.clear();
        mSavePlan.addAll(db.getSavePlan());
        mAdapter.notifyDataSetChanged();
    }

}